<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function return_data()
{

}

function generate_code($length)
{
    $str = null;
    $strPol = "0123456789";
    $max = strlen($strPol) - 1;

    for ($i = 0;
         $i < $length;
         $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    return $str;

}

function generate_task_no()
{
    $base = '2018-12-01';
    $y = date('Y');
    $m = date('m');
    $d = date('d');
    $month = ($y-2019)*12 + $m + 10;
    $random = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);;
    return $month.$d.$random.generate_code(3);
}

//生成邀请码
function generate_request_code(){
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $max = strlen($strPol) - 1;

    for ($i = 0;
         $i < 2;
         $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    $random = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);;
    return $str.$random;

}


//获取设定长度随机字符串
function get_rand_char($length)
{
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;

    for ($i = 0;
         $i < $length;
         $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    return $str;
}


/**
 * @param string $url get请求地址
 * @param int $httpCode 返回状态码
 * @return mixed
 */
function curl_get($url, &$httpCode = 0)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //不做证书校验,部署在linux环境下请改为true
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_contents = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

//登录密码加密
function encpwd($pwd,$salt){
    return md5($pwd.$salt);
}


//充值订单号
function generate_order_no()
{
    $base = '2018-12-01';
    $y = date('Y');
    $m = date('m');
    $d = date('d');
    $month = ($y-2019)*12 + $m + 10;
    $random = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);;
    $str =  $month.$d.$random.generate_code(3);
    $res = \app\api\model\Recharge::where('order_no',$str)->field('id')->find();
    if ($res){
        $str = generate_order_no();
    }
    return $str;
}